var config = require("config");
//道具组
var propG = cc.Class({
    name:'propG',
    properties: {
        name:'',
        freqTime:0,
        initPollCount:0,
        prefab:cc.Prefab
    }
});

cc.Class({
    extends: cc.Component,

    properties:()=>({
        propG: {
            default:[],
            type: propG
        },
        main: {
            default: null,
            type: require('main'),
        },
    }),
    
    // use this for initialization
    onLoad: function () { 
        //初始化道具组
        this.eState = D.commonInfo.gameState.none;
        D.common.batchInitObjPool(this,this.propG);
    },
    startAction: function(newV2){
        this.eState = D.commonInfo.gameState.start;
        var index = Math.floor(Math.random()*10);//
        if(index != 1){
            index = 0;
        }
        this.getNewEnemy(this.propG[index],newV2);
    },
    //重新开始
    resumeAction: function(){
        this.enabled = true;
        this.eState = D.commonInfo.gameState.start;
    },
    //暂停
    pauseAction: function(){
        this.enabled = false;
        this.eState = D.commonInfo.gameState.pause;
    },
    //生成道具
    getNewEnemy: function(enemyInfo,newV2) {
        var poolName = enemyInfo.name + 'Pool';
        var newNode = D.common.genNewNode(this[poolName],enemyInfo.prefab,this.node);
        newNode.setPosition(newV2);
        newNode.getComponent('prop').init();
    },
    //道具随机生成的位置
    getNewEnemyPositon: function(newEnemy) {
        //位于上方，先不可见
        var randx = Math.random()*(this.node.parent.width/2-newEnemy.width/2);
        var randy = this.node.parent.height/2+newEnemy.height/2;
        return cc.v2(randx,randy);
    },
    enemyDied: function(nodeinfo,score){
        //回收节点
        D.common.backObjPool(this,nodeinfo);
        //增加分数
        if (parseInt(score)>0){
            var gold = parseInt(score);
            if(config.CurHeroIndex == 3 || config.CurHeroIndex == 5){
                gold = gold*1.5;
            }
            this.main.gameCoinAdd(parseInt(gold));
        }  
    },
    getScore: function(){
        return this.main.getScore();
    },
    //获取英雄位置
    getHeroPos: function(){
        return this.main.getHeroPos();
    },
    //吃到道具效果
    //冲刺
    getOnrush: function(){
        this.main.heroOnrush();
    },
    //双倍子弹
    getDoubleBullet: function(){
        if (config.HeroState==0)
            this.main.bulletGroup.changeBullet('ufoBullet');
    },
    //吸铁石
    getMagnet: function(){
        if(config.CurHeroIndex == 4 || config.CurHeroIndex == 5){
            return;
        }
        var self = this;
        config.isToHero = true;
        this.node.stopAllActions();
        var delay = cc.delayTime(8);
        this.main.hero.setMangetXiAni(true);
        var callFunc = cc.callFunc(function(){
            config.isToHero = false;
            self.main.hero.setMangetXiAni(false);
        });
        this.node.runAction(cc.sequence(delay,callFunc));
    },
    //激光
    getLaser: function(){
        this.main.getUfoBomb();
    }
});
